package org.example.utils.rocketmq;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.base.BaseResp;
import org.example.constants.Constants;
import org.example.config.RocketMqQueueSelector;
import org.example.model.messagequeue.SendMessageReq;
import org.example.utils.RocketMqTestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author huangy
 * @description
 * @since 2022/11/9 19:37
 **/
@RestController
@RequestMapping("/rocketmq")
public class RocketMqSend {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Resource
    private RocketMqQueueSelector queueSelector;

    @Resource
    private RocketMqTestUtils rocketMqTestUtils;

    @Value("${message.queue.topic}")
    private String topic;

    /*
     * @description 发送信息 请求不带数据信息
     * @author huangy
     * @since 2022/11/9 20:19
     * @return org.example.common.base.BaseResp<?>
     **/
    @PostMapping("send")
    public BaseResp<?> sendMessage() {
        SendResult sendResult = rocketMQTemplate.syncSend(topic + ":"
                + Constants.RocketMqConstants.TAG_TEST, "hello world");
        return BaseResp.success(sendResult);
    }

    /*
     * @description 发送信息 请求带数据信息
     * @author huangy
     * @since 2022/11/9 20:19
     * @param req
     * @return org.example.common.base.BaseResp<?>
     **/
    @PostMapping("sendMsg")
    public BaseResp<?> sendMessage(@RequestBody SendMessageReq req) {
        String reqJson = JSON.toJSON(req).toString();
        SendResult sendResult = rocketMQTemplate.syncSend(topic + ":"
                + Constants.RocketMqConstants.TAG_TEST, reqJson);
        return BaseResp.success(sendResult);
    }

    /*
     * @description 发送随机消息
     * @author huangy
     * @since 2022/11/10 17:49
     * @return org.example.common.base.BaseResp<?>
     **/
    @PostMapping("sendRandomMsg")
    public BaseResp<?> sendRandomMessage() {
        rocketMqTestUtils.sendRandomMessage();
        return BaseResp.success();
    }

}
